package com.shan.domm.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shan.domm.common.BaseResponse;
import com.shan.domm.common.ErrorCode;
import com.shan.domm.common.ResultUtils;
import com.shan.domm.exception.BusinessException;
import com.shan.domm.mapper.ApplyMapper;
import com.shan.domm.model.domain.Apply;
import com.shan.domm.model.domain.Room;
import com.shan.domm.service.ApplyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/apply")
@Slf4j
public class ApplyController {

    @Resource
    private ApplyService applyService;

    @Resource
    private ApplyMapper applyMapper;

    /**
     * 增加，删除，同意，不同意
     */
    @PostMapping(value = "/add")
    public BaseResponse AddApply(@RequestBody Apply apply){
        if(apply==null){
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);
        }
        String s = applyService.AddApply(apply);
        return ResultUtils.success(s);
    }
    @PostMapping(value = "/delete")
    public BaseResponse<String> DeleteApply(@RequestBody Apply apply){
        if(apply==null){
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);
        }
        String s = applyService.deleteApply(apply);
        return ResultUtils.success(s);
    }
    @PostMapping(value = "/agree")
    public BaseResponse<String> agree(@RequestBody Apply apply){
        if(apply==null){
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);
        }
        String agree = applyService.agree(apply);
        return ResultUtils.success(agree);
    }
    @PostMapping(value="/refuse")
    public BaseResponse<String> refuse(@RequestBody Apply apply){
        if(apply==null){
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);
        }
        String refuse = applyService.refuse(apply);
        return ResultUtils.success(refuse);
    }
    @PostMapping(value = "/getapply")
    public BaseResponse<Apply> getApplyById(String id){
        if(id==null){
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);
        }
        Apply applynet = applyService.getById(id);
        return ResultUtils.success(applynet);
    }
    @PostMapping(value="/getapplys")
    public BaseResponse<Page<Apply>> getApplyList(Integer current,Integer size){
        if(current==null||size==null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"参数为空");
        }
        Page<Apply> page=new Page<>(current,size);
        Page<Apply> pageList=applyMapper.selectPage(page,null);
        return ResultUtils.success(pageList);
    }
}
